/**
*常用函数
*/
import qs from 'qs';
import axios from '../axiosConf/axios';
/**
 * @description axios事件 post(url,params)
 */
export const post = async  (url,params)=>{
    let postData = qs.stringify(params);
    let parameter = {
        method:"post",
        url:url,
        data:postData
    };
    let response = await axios(parameter);
    return response.data;
};
/**
 * @description axios事件 get(url,params)
 */
export const get = async  (url,params)=>{
    let postData = qs.stringify(params);
    let parameter = {
        method:"get",
        url:url,
        data:postData
    };
    let response = await axios(parameter);
    return response.data;
};
/**
 * @description 绑定事件 on(element, event, handler)
 */
export const on = ()=>{
    if (document.addEventListener) {
        return function (element, event, handler) {
            if (element && event && handler) {
                element.addEventListener(event, handler, false)
            }
        }
    } else {
        return function (element, event, handler) {
            if (element && event && handler) {
                element.attachEvent('on' + event, handler)
            }
        }
    }
};

/**
 * @description 解绑事件 off(element, event, handler)
 */
export const off = ()=>{
    if (document.removeEventListener) {
        return function (element, event, handler) {
            if (element && event) {
                element.removeEventListener(event, handler, false)
            }
        }
    } else {
        return function (element, event, handler) {
            if (element && event) {
                element.detachEvent('on' + event, handler)
            }
        }
    }
};
/**
 * @description 时间戳格式化
 * @param {Date}  time 时间戳
 * @returns {String} 年月日时分秒
 */
export const getTDate = (time)=> {
    let date = '';
    if(time){
        date = new Date(time);
    }else{
        date =  new Date(new Date().getTime());
    }
    let yyyy = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let dd = date.getDate();
    dd = dd < 10 ? ('0' + dd) : dd;
    let HH = date.getHours();
    HH = HH < 10 ? ('0' + HH) : HH;
    let mm = date.getMinutes();
    mm = mm < 10 ? ('0' + mm) : mm;
    let ss = date.getSeconds();
    ss = ss < 10 ? ('0' + ss) : ss;
    let str =
        yyyy + '-' +
        MM + '-' +
        dd + '  ' +
        HH + ':' +
        mm + ':' +
        ss;
    return str
};
/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export const deepClone = (source)=>{
    if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'deepClone');
    }
    const targetObj = source.constructor === Array ? [] : {};
    Object.keys(source).forEach(keys => {
        if (source[keys] && typeof source[keys] === 'object') {
            targetObj[keys] = deepClone(source[keys])
        } else {
            targetObj[keys] = source[keys]
        }
    });
    return targetObj
};
/**
 * @description 数组去重
 * @param {Array} arr
 * @returns {Array}
 */
export const uniqueArr = (arr)=> {
    return Array.from(new Set(arr))
};
/**
 * @description 根据数组对象某个属性去重
 * @param {Array} arr
 * @param {String} field
 * @returns {Array}
 */
export const uniqueArrByField = (arr,field)=>{
    let obj = {};
    let new_arr = arr.reduce((item, next) =>{
        if(!obj[next[field]]){
            obj[next[field]] = true;
            item.push(next);
        }
        // obj[next[field]]?'':obj[next[field]] = true && item.push(next);
        return item;
    },[]);
    return new_arr;
};



